feat(subagent): rescan a session as soon as its CLI reports idle - #155
Merged
Conversation
The stability clock in detectSubagentTransitions only advances when the scan runs. PR #153 gave it a 5 s settle tick so a silent folder can no longer stall it; the remaining lateness is up to one tick plus the rest of the stability window. The Claude CLI already publishes a per-session state file at ~/.claude/sessions/<pid>.json whose status flips to idle when a turn ends. Watching that directory gives an earlier, more precise trigger for the same scan, at no cost while nothing changes state. The idle edge is a trigger, never a verdict: nothing here marks a subagent complete or emits subagent-completed, and every guard failure degrades to doing nothing so the settle tick stays the safety net. The file is not a documented interface, so a canary test pins its shape and skips itself where the CLI is absent.
devsuitup
force-pushed
the
feat/cli-session-state-rescan
branch
from
August 23, 2026 23:47
dca9342 to
8a4174d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
detectSubagentTransitionsadvances its stability clock only when it runs. #153 gave it a 5 s settle tick so a silent folder can no longer stall it; the lateness that remains is up to one tick plus the rest of the stability window.The Claude CLI already publishes a per-session state file at
~/.claude/sessions/<pid>.jsonwhosestatusflips toidlewhen a turn ends. Watching that directory is an earlier and more precise trigger for the same scan, and costs nothing while nothing changes state — onefs.watchover a handful of tiny files, no polling, no timer armed at rest (ADR 0002).The idle edge is a trigger, never a verdict. Nothing in the new module marks a subagent complete or emits
subagent-completed—grepfor either incli-session-state.jsreturns nothing. The stability clock stays the sole judge.Guards
procStartcatches pid reuse — a changed value reseeds the entry as a new process, so the accompanying status change is not read as a transition.Matching is by
sessionIdagainstrealSessionId || key, so forked and resumed sessions keep working —realSessionIdis load-bearing twice, since after a fork the CLI writes the new id whileactiveSessionsis still keyed by the old one, and it is also the id thesubagents/directory is named after.cwdis deliberately not used as a fallback: several sessions can share a working directory.Every failure — missing directory, truncated JSON, unknown status, no matching session — degrades to doing nothing, with #153's tick as the safety net.
Canary
~/.claude/sessions/<pid>.jsonis not a documented interface.test/canary-cli-session-state.test.jspins its shape and skips itself where the CLI is absent, so it is never red on CI or on a machine without the CLI. A CLI-side change then reads as a CLI-side change instead of starting a bug hunt here:This is the repo's first
canary-*.test.js; the convention is written up in.ai/contexts/cli-session-state.md.Reservation, recorded rather than glossed over
The
waitingbranch was never observed empirically — no permission dialog occurred during the 10-minute, 295-sample run used to characterise the file. It is treated as not-idle on the strength of its name alone; being wrong there only costs a missed early rescan, never a wrong verdict.Tests
12 new (11 behavioural + the canary), each proven by reverting the corresponding guard:
The skip path was verified by pointing
HOMEat an empty directory:pass 0, fail 0, skipped 1.725 tests, 718 pass, 0 fail, 7 pre-existing skips. ESLint: 0 errors, 265 warnings, unchanged — the three new files contribute none.